package com.company.nuwa.common.utils;

import cn.hutool.core.lang.UUID;
import cn.hutool.extra.spring.SpringUtil;

/**
 * <p></p >
 *
 * @author youyoukele
 * @version 1.0
 * @date 2023/8/25 14:55
 */
public class TraceUtils {

    public static String genTraceId() {
        String timestamp = String.valueOf(System.currentTimeMillis());
        String s = String.format("%s_%s_%s",
                SpringUtil.getApplicationName(),
                UUID.fastUUID().toString(Boolean.TRUE),
                // 取毫秒数即可，保留末尾五位即可
                timestamp.substring(timestamp.length() - 5)
        );
        // 去除 xxxx-xxxx-service 中的service
        s = s.replaceAll("service", "");
        // 去除 xxxx-xxxx中的 -
        s = s.replaceAll("-", "");
        // 简写 gateway为gw
        s = s.replaceAll("gateway", "gw");
        return s;
    }

}
